package com.finance.controller.admin.common;

import com.finance.common.Result;
import com.finance.pojo.admin.Admin;
import com.finance.pojo.user.User;
import com.finance.service.login.LoginService;
import com.finance.service.user.userinfo.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private LoginService loginService;

    /**
     * 管理员主界面，用户在线状态
     * @param pageNum
     * @param pageSize
     * @param label
     * @param target
     * @param model
     * @return
     */
    @RequestMapping("/admin/index.html")
    public String adminMain(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
                            @RequestParam(value = "label", defaultValue = "0") Integer label,//接收的要搜索的属性
                            @RequestParam(value = "target", defaultValue = "") String target,//接收的输入内容
                            Model model){
        //引入pageHelper插件
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userInfoService.selectAllUser();
        switch (label){
            case 1: userList = userInfoService.selectUserByUsername(target);break;
            case 2: userList = userInfoService.selectUserByRealname(target);break;
            case 3: userList = userInfoService.selectUserByPhone(target);break;
            case 4: userList = userInfoService.selectUserByEmail(target);break;
            default: break;
        }

        //PageInfo封装分页信息
        PageInfo<User> pageInfo = new PageInfo<User>(userList);
        model.addAttribute("userPageInfo", pageInfo);
        model.addAttribute("userList", userList);
        model.addAttribute("activeUrl", "indexActive");

        return "admin/main";
    }

    /**
     * 前往添加新的管理员界面
     * @return
     */
    @RequestMapping("/admin/toAddAdmin.html")
    public String addAdmin(){
        return "admin/adminAccount/addadmin";
    }


    /**
     * 验证管理员用户名是否存在
     * @param username
     * @param request
     * @return
     */
    @RequestMapping("/admin/VerifyUsername/{username}")
    @ResponseBody
    public Result VerifyUsername(@PathVariable("username") String username, HttpServletRequest request){
        int i = loginService.selectByName(username);
        if( i != 0){
            request.getSession().setAttribute("username",username);
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * 注册一个管理员，向数据库中添加一条新的管理员账号
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/admin/addAdmin")
    @ResponseBody
    public Result registerAdmin(@RequestParam("username") String username,
                                @RequestParam("password") String password,
                                HttpSession session){
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setStatus(0);
        int i = loginService.insertAdmin(admin);
        admin = loginService.adminLogin(admin);
        System.out.println("得到注册信息"+admin);
        if(i!= 0 && admin != null) {
            return Result.success();
        }else{
            return Result.fail();
        }
    }

    /**
     * 前往编辑管理员账户信息界面
     * @return
     */
    @RequestMapping("/admin/toEditAdmin.html")
    public String editAdminInfo(){
        return "admin/adminAccount/editadmin";
    }

    /**
     * 注销一个管理员账户
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/admin/destroyAccount")
    @ResponseBody
    public Result destroyAccount(@RequestParam("password") String password,HttpSession session){
        Admin admin = (Admin)session.getAttribute("loginAdmin");
        if(password.equals(admin.getPassword())){
            int i = loginService.deleteAdmin(admin.getId());
            if(i==1)
                return Result.success();
            else
                return Result.fail();
        }else {
            return Result.fail();
        }

    }

    /**
     * 更新管理员账户的密码
     * @param id
     * @param oldpwd
     * @param newpwd
     * @param session
     * @return
     */
    @RequestMapping("/admin/updatePwd")
    @ResponseBody
    public Result updatePassword(@RequestParam("id") Integer id,@RequestParam("oldpwd") String oldpwd,
                                 @RequestParam("newpwd") String newpwd,HttpSession session){
        Admin admin = (Admin)session.getAttribute("loginAdmin");
        if (admin == null){
            return Result.fail();
        }else {
            if (oldpwd.equals(admin.getPassword())){
                admin.setPassword(newpwd);
                int i = loginService.updateAdminPwd(admin);//更新数据库
                System.out.println("========updateAdminPwd========");
                session.setAttribute("loginAdmin",admin);
                if (i==1){
                    return Result.success();
                }else {
                    return Result.fail();
                }
            }else {
                return Result.fail();
            }
        }

    }

}
